package de.tiemannjan.android.util;

public abstract class Time {
	private long time;
	
	public static final int SECONDS_PER_HOUR = 3600;
	public static final int MILLISECONDS_PER_HOUR = 3600000;
	public static final int MILLISECONDS_PER_MINUTE = 60000;
	public static final int MILIISECONDS_PER_SECOND = 1000;
	public static final int MILIISECONDS_PER_MICROSECOND = 100;
	
	public void setTime(long time){
		// TODO: What happens in case of time<0?
		this.time = time;
	}
	
	public long getTime(){
		return this.time;
	}
	
	public abstract long getMilliSeconds();
	
	public CharSequence getTimeFormatted(){ 
		StringBuilder timeFormatted = new StringBuilder();
		long timeLeft = this.getMilliSeconds();
		long timeTmp = 0;
		
		// calculate full hours and append to return value
		timeTmp = timeLeft / Time.MILLISECONDS_PER_HOUR;
		timeFormatted.append(this.formatDigit(timeTmp));
		
		timeLeft -= timeTmp * Time.MILLISECONDS_PER_HOUR;
		
		// calculate full minutes and append to return value
		timeTmp = timeLeft / Time.MILLISECONDS_PER_MINUTE;
		timeFormatted.append(":" + this.formatDigit(timeTmp));
		
		timeLeft -= timeTmp * Time.MILLISECONDS_PER_MINUTE;
		
		// calculate full seconds and append to return value
		timeTmp = timeLeft / Time.MILIISECONDS_PER_SECOND;
		timeFormatted.append(":" + this.formatDigit(timeTmp));
		
		timeLeft -= timeTmp * Time.MILIISECONDS_PER_SECOND;
		
		// calculate full microseconds and append to return value
		timeTmp = timeLeft / Time.MILIISECONDS_PER_MICROSECOND;
		timeFormatted.append("." + timeTmp);
		
		return timeFormatted.toString();
	}
	
	private CharSequence formatDigit(long digit){
		if (digit < 10) {
			return "0" + digit;
		}else{
			return "" + digit;
		}
		
	}
	
}
